/**
 * 伪造IP，下载图片
 * @type {_|exports|module.exports}
 * @private
 */
var _ = require('underscore');
var cheerio = require('cheerio');
var superagent = require('superagent');
var async = require('async');
var mkdirp = require('mkdirp');
var fs = require('fs');
var http = require('http');

//本地存储目录
var dir = './tt1';

//创建目录
mkdirp(dir, function (err) {
    if (err) {
        console.log(err);
    }
});


//var url = "http://findicons.com/pack/5/animals?q=Search+in+icon+pack..."; //头像
var url = "http://findicons.com/pack/21/tiny_animals?q=Search+in+icon+pack..."; //头像

getData(url);

function getData(url) {

    console.log("url", url)

    superagent.get(url).end(function (err, sres) {
        if (err) {
            console.log(err)
            return;
        }

        var $ = cheerio.load(sres.text);
        var icon_list = $(".icon_list");
        var dds = icon_list.find("dd");
        var pages = new Array();
        _.each(dds, function (item, index) {

            var href = $(item).find("a").eq(0).attr("href");
            pages.push(href);

        })

        console.log(pages.length)

        async.eachSeries(pages, function (item, callback) {

            getHtml(item, function () {
                callback(null)
            });


        }, function (err, result) {

            if (err) {
                console.log("-一轮结束-" + err)
            }
        });

    });

}



function getHtml(href, getHtmlCB) {

    var temp = "http://findicons.com/" + href;

    console.log(temp)

    superagent.get(temp).end(function (err, sres) {
        if (err) {
            console.log(err)
            return;
        }

        var $ = cheerio.load(sres.text);

        var name = $(".tag_btn").find("a").eq(0).attr("title");
        if (name) {
            name = name.replace(/ /g, "_")
        }

        var imgs = $(".other-size").find("img");

        _.each(imgs,function(img , index){

            if(index < 3){
                var src = $(img).attr("src");
                var tempName = name;
                if(index == 0){
                    tempName += "@3x.png"
                }else if(index == 1){
                    tempName += "@2x.png"
                }else if(index == 2){
                    tempName += ".png"
                }
                saveImagea(src, dir, tempName, function () {
                })
            }
        })

        getHtmlCB()


    });

}


/**
 * 获取最大图片
 * @param href
 * @param getHtmlCB
 */
function getHtmlBig(href, getHtmlCB) {

    var temp = "http://www.easyicon.net" + href;

    console.log(temp)

    superagent.get(temp).end(function (err, sres) {
        if (err) {
            console.log(err)
            return;
        }

        var $ = cheerio.load(sres.text);

        var sub_img_box = $(".sub_img_box");

        var name = $(".icon_img_one").find("img").attr("alt");
        if (name) {
            name = name.replace(/ /g, "_")
        }

        var href = $(".img_detail_one").find("h4").find("a").eq(0).attr("href");


        if (href) {
            saveImagea(href, dir, name + ".png", function () {
                getHtmlCB();
            })
        } else {
            getHtmlCB();
        }


    });

}




/**
 * 伪造ip，下载图片
 * @param url
 * @param dir
 * @param name
 * @param cb
 */
var saveImagea = function (url, dir, name, cb) {

    var ip = _.random(1, 254)
        + "." + _.random(1, 254)
        + "." + _.random(1, 254)
        + "." + _.random(1, 254)

    console.log(url, ip)
    superagent
        .get(url)
        //.set("X-Forwarded-For" , "10.111.198.90")
        .set("X-Forwarded-For", ip)
        .end(function (err, sres) {
            if (err) {
                console.log(err)
                return;
            }
            //console.log(sres.body);
            fs.writeFile(dir + "/" + name, sres.body, 'binary', function (err) {
                if (err) throw err;
                console.log('file saved ' + name);
                cb();
            });
        })

}
